package com.syz.web.controller;




import com.syz.web.pojo.Student;
import com.syz.web.service.IStudentService;
import com.syz.web.service.Impl.StudentServiceImpl;
import com.syz.web.util.JSONUtil;
import com.syz.web.util.PageInfo;
import com.syz.web.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    IStudentService studentService=new StudentServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectAll":
                selectAll(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "getStudentUpdatePage":
                getStudentUpdatePage(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
        }
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNoStr = req.getParameter("pageNo");
        if (pageNoStr == null || pageNoStr.equals("")) {
            pageNoStr = "1";
        }
        String pageSizeStr = req.getParameter("pageSize");
        if (pageSizeStr == null || pageSizeStr.equals("")) {
            pageSizeStr = "10";
        }
        int pageNo = Integer.parseInt(pageNoStr);
        int pageSize = Integer.parseInt(pageSizeStr);

        PageInfo<Student> pageInfo = studentService.selectByPage(pageNo, pageSize);

        req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("Student/student_list.jsp").forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String address=req.getParameter("address");
        String banjiId=req.getParameter("banjiId");
        boolean isSuccess=studentService.add(name,age,gender,banjiId,address);
        Result result = isSuccess ? Result.ok("添加成功") : Result.error("添加失败");
        JSONUtil.toJSON(resp, result);
        //resp.sendRedirect(req.getContextPath() + "/student");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");

        boolean success=studentService.deleteById(id);

        resp.sendRedirect(req.getContextPath() + "/student");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Student> list = new ArrayList<>();
        list=studentService.selectAll();

        req.setAttribute("list", list);
        req.getRequestDispatcher("Student/student_list.jsp").forward(req, resp);
    }
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String address=req.getParameter("address");

        boolean success=studentService.update(id,name,age,gender,address);

        resp.sendRedirect(req.getContextPath() + "/student");
    }
    private void getStudentUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

        Student student = null;
        student=studentService.getStudentUpdatePage(id);
        //把list数据放到req里面
        req.setAttribute("student", student);
        //转发到student_list.jsp页面进行展示
        //System.err.println(student);
        req.getRequestDispatcher("Student/student_update.jsp").forward(req, resp);
    }

}
